package com.zhl.controller;

import com.zhl.entity.Message;
import com.zhl.entity.User;
import com.zhl.entity.ZhlResult;
import com.zhl.service_.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/10/12.
 * 留言板
 */
@Controller
public class MessageController {

    Logger logger = LoggerFactory.getLogger("MessageController");
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private MessageService messageService;

    /**
     * 留言板查询所有留言
     * @return
     */
    @RequestMapping("/api/message")
    @ResponseBody
    public ZhlResult messageList() {

        //查询所有主留言
        List<Message> messageList = messageService.messageList();

        //查询所有主留言下的回复
        for (Message message:messageList) {
            if (message.getHaschild() == 1) {
                List<Message> childMessageList = messageService.childMessageList(message.getId());
                message.setChild(childMessageList);
            }
        }


        ZhlResult zhlResult = ZhlResult.ok(messageList);
        zhlResult.setTotalSize(messageList.size());
        return zhlResult;

    }

    /**
     * 添加留言
     * @param request
     * @param message
     * @return
     */
    @RequestMapping("/api/addmessage")
    @ResponseBody
    public ZhlResult addMessage(HttpServletRequest request, Message message) {

        User user = (User)request.getSession().getAttribute("user");

        if(message.getContext() == null || message.getContext() == "") {
            return ZhlResult.build(400,"留言不能为空");
        }
        String formatDate = this.format.format(new Date());
        message.setCreateTime(formatDate);
        message.setUsername(user.getUsername());
        message.setIcon(user.getIcon());
        message.setNickname(user.getNickname());

        messageService.addMessage(message);

        return ZhlResult.ok();
    }


    /**
     * 查询文章下的评论
     * @return
     */
    @RequestMapping("/api/comment/{belongArticle}")
    @ResponseBody
    public ZhlResult commentList(@PathVariable("belongArticle") int belongArticle) {

        //查询所有主留言
        List<Message> messageList = messageService.commentList(belongArticle);

        //查询所有主留言下的回复
        for (Message message:messageList) {
            if (message.getHaschild() == 1) {
                List<Message> childMessageList = messageService.childMessageList(message.getId());
                message.setChild(childMessageList);
            }
        }

        ZhlResult zhlResult = ZhlResult.ok(messageList);
        zhlResult.setTotalSize(messageList.size());
        return zhlResult;

    }


    //删除留言
    @GetMapping("/api/deleteMessage/{id}")
    @ResponseBody
    public ZhlResult deleteMessage(@PathVariable("id")int id) {
        logger.info("删除留言");

        int i = messageService.deleteMessage(id);
        if (i == 1 ) {
            ZhlResult zhlResult = ZhlResult.build(400,"删除失败");
            return zhlResult;
        }else {
            ZhlResult zhlResult = ZhlResult.ok();
            return zhlResult;

        }

    }




}
